import Vue from 'vue'
import Vuex from 'vuex'
import common from '../utils/common'

Vue.use(Vuex)

// types
const types = {
  SET_USERINFO: "SET_USERINFO",
  SET_LOATION: "SET_LOCATION",
  SET_ADDRESS: "SET_ADDRESS",
  SET_CART: "SET_CART",
  ADD_CART: "ADD_CART",
  SET_ORDER: "SET_ORDER",
  SET_ACTION: "SET_ACTION",
  SET_STOREINFO: "SET_STOREINFO",
  SET_ISTAKESELF: "SET_ISTAKESELF",
  SRT_CURRENTADDRESS: "SRT_CURRENTADDRESS"
}

//state
const state = {
  //用户信息
  userInfo: null,
  //外卖或自取
  isTakeSelf: 1,
  //店面
  storeInfo: JSON.stringify({}),
  // 底部导航条
  action: 0,
  location: {},
  address: "",
  //totalCount表示所有商品，包括重叠的
  cart: JSON.stringify({ commodityList: [], totalPrice: 0, totalCount: 0, selectedGroup: 0 }),
  orderData: JSON.stringify({ commodityList: [], tel: '', isBag: 0, remark: "口味、包装等要求" }),
  //当前选定的收货地址
  currentAddress: JSON.stringify({})
}

//getters
const getters = {
  userInfo: state => JSON.parse(state.userInfo),
  storeInfo: state => JSON.parse(state.storeInfo),
  action: state => state.action,
  location: state => state.location,
  address: state => state.address,
  cart: state => JSON.parse(state.cart),
  orderData: state => JSON.parse(state.orderData),
  isTakeSelf: state => state.isTakeSelf,
  currentAddress: state => JSON.parse(state.currentAddress),
}

//mutations
const mutations = {
  [types.SET_USERINFO](state, user) {
    if (user) {
      state.userInfo = JSON.stringify(user);
    } else {
      state.userInfo = null;
    }
  },
  [types.SET_LOATION](state, location) {
    if (location) {
      state.location = location;
    } else {
      state.location = null;
    }
  },
  [types.SET_ADDRESS](state, address) {
    if (address) {
      state.address = address;
    } else {
      state.address = "";
    }
  },
  [types.SET_CART](state, cart) {
    if (JSON.stringify(cart) != "{}") {
      state.cart = JSON.stringify(cart);
    } else {
      state.cart = JSON.stringify({ commodityList: [], totalPrice: 0, totalCount: 0, selectedGroup: 0 });
    }
  },
  [types.ADD_CART](state, commodity) {
    var cart = JSON.parse(state.cart);
    var index = common.isExist(cart, commodity);
    var isExist = index != -1;
    if (isExist) {
      cart.totalCount = parseInt(cart.totalCount) + parseInt(commodity.count);
      cart.totalPrice =
        parseFloat(cart.totalPrice) +
        parseFloat(commodity.price) * parseInt(commodity.count);
      cart.commodityList[index].count =
        parseInt(cart.commodityList[index].count) + parseInt(commodity.count);
    } else {
      cart.totalCount = parseInt(cart.totalCount) + parseInt(commodity.count);
      cart.totalPrice =
        parseFloat(cart.totalPrice) +
        parseFloat(commodity.price) * parseInt(commodity.count);
      cart.commodityList.push(commodity);
      cart.selectedGroup = parseFloat(cart.selectedGroup) + parseInt(1)
    }
    state.cart = JSON.stringify(cart)
  },
  [types.SET_ORDER](state, orderDate) {
    if (JSON.stringify(orderDate) == "{}") {
      state.orderData = JSON.stringify({ commodityList: [], tel: '', couponId: '', isBag: 0, remark: "口味、包装等要求" })
    } else {
      state.orderData = JSON.stringify(orderDate)
    }
  },
  [types.SET_ACTION](state, action) {
    state.action = action
  },
  [types.SET_STOREINFO](state, store) {
    if (store) {
      state.storeInfo = JSON.stringify(store);
    } else {
      state.storeInfo = null;
    }
  },
  [types.SET_ISTAKESELF](state, isTakeSelf) {
    state.isTakeSelf = isTakeSelf;
  },
  [types.SRT_CURRENTADDRESS](state, currentAddress) {
    if (JSON.stringify(currentAddress) != "{}") {
      state.currentAddress = JSON.stringify(currentAddress);
    } else {
      state.currentAddress = JSON.stringify({});
    }
  },
}

//actions
const actions = {
  setUserInfo: ({ commit }, user) => {
    commit(types.SET_USERINFO, user);
  },
  setLocation: ({ commit }, location) => {
    commit(types.SET_LOATION, location);
  },
  setAddress: ({ commit }, address) => {
    commit(types.SET_ADDRESS, address);
  },
  setCart: ({ commit }, cart) => {
    commit(types.SET_CART, cart);
  },
  addCart: ({ commit }, commodity) => {
    commit(types.ADD_CART, commodity);
  },
  setOrder: ({ commit }, orderData) => {
    commit(types.SET_ORDER, orderData);
  },
  setAction: ({ commit }, action) => {
    commit(types.SET_ACTION, action);
  },
  setStoreInfo: ({ commit }, store) => {
    commit(types.SET_STOREINFO, store);
  },
  setIsTakeSelf: ({ commit }, isTakeSelf) => {
    commit(types.SET_ISTAKESELF, isTakeSelf);
  },
  setCurrentAddress: ({ commit }, currentAddress) => {
    commit(types.SRT_CURRENTADDRESS, currentAddress);
  },
}

export default new Vuex.Store({
  state,
  getters,
  mutations,
  actions
})
